package cn.wsn2018.tools.dao.impl;

import cn.wsn2018.tools.dao.SystemConfigDao;
import cn.wsn2018.tools.model.SystemConfig;
import cn.wsn2018.tools.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;


/**
 * @program: tools
 * @description
 * @author: songDaisong
 * @create: 2020-01-15 15:13
 **/
@Repository
public class SystemConfigImpl implements SystemConfigDao {

    @Autowired
    private  MongoTemplate mongoTemplate;

    @Override
    public boolean isExistence() {
        /**
         * 查询系统配置表是否存在
         * */
        Query query = new Query();
        long count = mongoTemplate.count(query, "secSystemConfig");
        if(count==0){
            System.out.println("tip:system config table is not existed");
            return false;
        }else{
            SystemConfig systemConfig = SystemConfig.getInstance();
            systemConfig.setSystemConfig(mongoTemplate.findOne(query, SystemConfig.class, "secSystemConfig"));
            System.out.println("tip:system config table is existed");
            return true;
        }

    }

    @Override
    public boolean initialSystemConfig() {
        //初始化系统配置表
        if(isExistence()){
            return true;
        }else{
            System.out.println("tip:begin create system config table");
            SystemConfig systemConfig = SystemConfig.getInstance();
            SystemConfig secSystemConfig = mongoTemplate.insert(systemConfig, "secSystemConfig");
            JSONObject jsonObject = JSONObject.fromObject(secSystemConfig);
            if(StringUtil.isEmptyString(jsonObject.optString("id"))){
                System.out.println("tip:falied to create system config table");
                return false;
            }else {
                System.out.println("tip:create system config table successfull");
                return true;
            }
        }

    }

    @Override
    public boolean updateSystemConfig(SystemConfig systemConfig) {
        Query query = new Query();
        Update update = new Update();
        query.addCriteria(Criteria.where("_id").is(SystemConfig.getInstance().getId()));
        //String notLoginTime=systemConfig.getNotLoginTime();
        String pwdValidTime=systemConfig.getPwdValidTime();
        String accountValidTime=systemConfig.getAccountValidTime();
        String loginFailedNum=systemConfig.getLoginFailedNum();
        String lockedTime=systemConfig.getLockedTime();
        String sessionMaxOverTime=systemConfig.getSessionMaxOverTime();
        String concurrentMaxNum=systemConfig.getConcurrentMaxNum();
        String logMaxVolume=systemConfig.getLogMaxVolume();
//        if(!StringUtil.isEmptyString(notLoginTime)){
//            update.set("notLoginTime",notLoginTime);
//        }
        if(!StringUtil.isEmptyString(pwdValidTime)){
            update.set("pwdValidTime",pwdValidTime);
        }
        if(!StringUtil.isEmptyString(accountValidTime)){
            update.set("accountValidTime",accountValidTime);
        }
        if(!StringUtil.isEmptyString(lockedTime)){
            update.set("lockedTime",lockedTime);
        }
        if(!StringUtil.isEmptyString(loginFailedNum)){
            update.set("loginFailedNum",loginFailedNum);
        }
        if(!StringUtil.isEmptyString(sessionMaxOverTime)){
            update.set("sessionMaxOverTime",sessionMaxOverTime);
        }
        if(!StringUtil.isEmptyString(logMaxVolume)){
            update.set("logMaxVolume",logMaxVolume);
        }
        if(!StringUtil.isEmptyString(concurrentMaxNum)){
            update.set("concurrentMaxNum",concurrentMaxNum);
        }
        long secSystemConfig = mongoTemplate.upsert(query, update, "secSystemConfig").getMatchedCount();

        //更新服务器内存中的系统配置对象
        systemConfig.setSystemConfig(systemConfig);
        //System.out.println(SystemConfig.getInstance());
        return true;
    }

    @Override
    public SystemConfig getSystemConfig() {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(SystemConfig.getInstance().getId()));
        SystemConfig secSystemConfig = mongoTemplate.findOne(query, SystemConfig.class, "secSystemConfig");
        //更新内存里面的系统配置对象
        SystemConfig.getInstance().setSystemConfig(secSystemConfig);
        //secSystemConfig.setId(null);
        return secSystemConfig;
    }


}
